<div id="container">
<section id="content">
<script>
	function func_back(){
		$("#main_form").prop("action", "manageListSubject");
		return $("#main_form").submit();
	}

	function displayChooseItem(){
		$("#fileSpan").html('<input type="file" name="file" id="file"/><br/><span id="fileMsg" class="errMsg"></span>');
		$("#btnChoose").css('display', 'none');
	}

	function func_submit(){
		
		var isValidName = true;
		var isValidVideo = true;
		var isValidFile = true;
		var isValidDesc = true;
		if($("#txtSubName").val() == ""){
			$("#nameMsg").html("(Vui lòng nhập tên môn học)");
			isValidName = false;
		}else{
			isValidName = true;
		}

   		if($("#txtVideoId").val() == ""){
   			$("#videoMsg").html("(Vui lòng nhập mã video)");
   			isValidVideo = false;
   		} else{
        	$("#videoMsg").html("");
        	isValidVideo = true;
        }

   		if ($("#file").length > 0){
   			if($("#file").val() == ""){
   				$("#fileMsg").html("(Vui lòng chọn ảnh đại diện cho môn học)");
   				isValidFile = false;
   	   		}else{
   	   	   		var fileName = $("#file").val();
   	   	   		if (fileName.indexOf(".") == -1){
   	   	   			$("#fileMsg").html("(Vui lòng chọn file hình ảnh [*.jpg, *.png hoặc *.gif] )");
   	   	   			isValidFile = false;
   	   	   		}else{
   					var extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.substring.lenght);
					
   					if(extension != "png" && extension != "PNG"  &&
   							extension != "jpg" && extension != "JPG" && extension != "JPEG" && 
   							extension != "gif" && extension != "GIF"){
   						$("#fileMsg").html("(Vui lòng chọn file hình ảnh [*.jpg, *.png hoặc *.gif] )");
   		   	   			isValidFile = false;
   					}else{
   						$("#fileMsg").html("");
   				    	isValidFile = true;
   					}
   	   	   		}
   		   	   	
   	        }
   		}
		
   		if($("#txtSubDescript").val() == ""){
   			$("#descMsg").html("(Vui lòng nhập nội dung mô tả)");
   			isValidDesc = false;
   		} else{
        	$("#descMsg").html("");
        	isValidDesc = true;
        }

   		if (!($.isNumeric($("#txtSubFee").val()))){
   			$("#feeMsg").html("(Học phí không hợp lệ)");
   			isValidDesc = false;
   		} else{
        	$("#feeMsg").html("");
        	isValidDesc = true;
        }
   		   		
   		if(isValidName && isValidVideo && isValidFile && isValidDesc){
   			var mydata = {};
			mydata['sub_name'] = $("#txtSubName").val();
			mydata['confirm_type'] = $("#hidConfirmType").val();
			mydata['old_sub_name'] = $("#hidOldSubName").val();

			if ($("#file").length != 0){
				var path = $("#file").val();
				mydata['image_name'] = path.substr(path.lastIndexOf('\\') + 1);
				mydata['subOldImage'] = $("#subOldImage").val();
			}
			
			$.ajax({
		        type: "POST", 
		        url: "/it_center/Subjects/checkExistSubject/", 
		        data: mydata,
		        success: function(data){  	
			        if(data == "fileError"){
				        $("#fileMsg").html("(File đã tồn tại)");
				        return;
			        }else if (data != ""){
			        	$("#nameMsg").html("("+data+")");
				        return;
			        }else{
			        	return $("#main_form").submit();
			        }
		       	} 
	   		});
		}	
	}

</script>


	<form id="main_form" method="post" action="confirmSubject" enctype="multipart/form-data">
	<input type="hidden" id="hidConfirmType" value="update" name="confirmType"/>
	<input type="hidden" id="hidFileChange" value="<?php echo isset($fileChange) ? $fileChange : "";?>" name="isFileChange"/>
	
	<?php if($isBack != ""){?>
		<input type="hidden" id="hidOldSubName" name="oldSubName"
			value="<?php echo $oldSubName; ?>" />
	<?php }else{?>
	 <input type="hidden" id="hidOldSubName" name="oldSubName"
		value="<?php echo $subject[0]['Subject']['sub_name']; ?>" />
	<?php }?>
	
	<?php if($isBack != ""){?>
		<input type="hidden" id="hidFile" name="fileName"
			value="<?php echo $imageName; ?>" />
		<input type="hidden" name="subOldImage" id="subOldImage" value="<?php echo $imageName?>" />
	<?php }else{?>
	 <input type="hidden" id="hidFile" name="fileName"
		value="<?php echo $subject[0]['Subject']['sub_image']; ?>" />
	<input type="hidden" name="subOldImage" id="subOldImage" value="<?php echo $subject[0]['Subject']['sub_image'];?>" />
	<?php }?>
	
	<?php if($isBack != ""){?>
		<input type="hidden" id="hidSubId" name="subId"
			value="<?php echo $subId; ?>" />
	<?php }else{?>
	 <input type="hidden" id="hidSubId" name="subId"
		value="<?php echo $subject[0]['Subject']['sub_id']; ?>" />
	<?php }?>
	
	<center>
		<H1>Cập Nhật Chương Trình Đào Tạo</H1>
	</center>

	<table id="edit_table">
		<tr>
			<td class="edit_column" width="170">Môn học</td>
			<td style="text-align: left;">
			<?php if($isBack != ""){?>
				<input type="text" id="txtSubName" name="subName"
				value="<?php echo $subName; ?>" />
				<span id="nameMsg" class="errMsg"></span>
			<?php }else{?>
				<input type="text" id="txtSubName" name="subName"
				value="<?php echo $subject[0]['Subject']['sub_name']; ?>" />
				<span id="nameMsg" class="errMsg"></span>
			<?php }?>
			</td>
		</tr>
		
		<tr>
			<td class="edit_column" width="170">Học phí</td>
			<td style="text-align: left;">
			<?php if($isBack != ""){?>
				<input type="text" id="txtSubFee" name="subFee"
				value="<?php echo $subFee; ?>" /> VNĐ
				<span id="feeMsg" class="errMsg"></span>
			<?php }else{?>
				<input type="text" id="txtSubFee" name="subFee"
				value="<?php echo $subject[0]['Subject']['sub_fee']; ?>" /> VNĐ
				<span id="feeMsg" class="errMsg"></span>
			<?php }?>
			</td>
		</tr>
		
		<tr>
			<td class="edit_column">Mã Video</td>
			<td>
			<?php if($isBack != ""){?>
				<input type="text" id="txtVideo" name="video_id"
				value="<?php echo $videoId; ?>" />
				<span id="nameMsg" class="errMsg"></span>
			<?php }else{?>
				<input type="text" id="txtVideo" name="video_id"
				value="<?php echo $video['Video']['m_youtube_id']; ?>" />
				<span id="nameMsg" class="errMsg"></span>
			<?php }?>
			</td>
		</tr>
		<tr>
			<td class="edit_column">Hình đại diện</td>
			<td>
				<?php if($isBack != ""){?>
					<span id="fileSpan"><?php echo $imageName; ?></span>
					<button type="button" id="btnChoose" onclick="displayChooseItem()">Chọn file</button>
				<?php }else{?>
					<span id="fileSpan"><?php echo $subject[0]['Subject']['sub_image']; ?></span>&nbsp;
					<button type="button" id="btnChoose" onclick="displayChooseItem()">Chọn file</button>
				<?php }?>
				<span id="fileMsg" class="errMsg"></span>
			</td>
		</tr>
		<tr>
			<td class="edit_column" style="vertical-align: top;">Mô tả môn học</td>
			<td style="text-align: left;">
				<?php if($isBack != ""){?>
				<textarea style="width: 99%;" rows="20" name="subDescript" id="txtSubDescript"><?php echo $subDescript; ?></textarea>
				<br/>
				<span id="descMsg" class="errMsg"></span>
				<?php }else{?>
				<textarea style="width: 99%;" rows="20" name="subDescript" id="txtSubDescript"><?php echo $subject[0]['Subject']['sub_descript']; ?></textarea>
				<br/>
				<span id="descMsg" class="errMsg"></span>
				<?php }?>
			</td>
		</tr>
		<tr>
			<td colspan="2" style="text-align: center;">
				<button type="button" onclick="func_submit()">Xác Nhận</button>
				<button type="button" onclick="func_back()" style="margin-left: 10px;">Quay Lại</button>
			</td>
		</tr>
	</table>
	</form>
</section>
</div>
